package cn.rengy.web.excelcompare.controller;

import cn.rengy.web.excelcompare.service.ExcelComporeService;
import cn.rengy.lang.ResultEntity;
import cn.rengy.tool.core.util._NumberUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
public class ExcelComporeController {

    @Autowired
    private ExcelComporeService excelComporeService;

    /**
     * 导出表1
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/compare/exportData1")
    public ResultEntity exportData1(@RequestBody Map<String,Object> param) throws Exception {
        Long taskId=MapUtils.getLong(param,"taskId");
        String tableName=MapUtils.getString(param,"tableName");
        //int sheetNumber= _NumberUtils.toInt(request.getParameter("sheetNumber"),1);
        //int dataStartRowNumber=_NumberUtils.toInt(request.getParameter("dataStartRowNumber"),1);
        return excelComporeService.exportExcelData("select_export_"+tableName,taskId);
    }
//    @RequestMapping("/compare/exportData2")
//    public ResultEntity exportData2(@RequestBody Map<String,Object> param) throws Exception {
//        Long taskId=MapUtils.getLong(param,"taskId");
//        //int sheetNumber= _NumberUtils.toInt(request.getParameter("sheetNumber"),1);
//        //int dataStartRowNumber=_NumberUtils.toInt(request.getParameter("dataStartRowNumber"),1);
//        return excelComporeService.exportExcelData("select_export_excel_data2",taskId);
//    }
    /**
     * 导入模板后设置表头
     * @param request
     * @param file
     * @return
     */
    @RequestMapping("/compare/setExcelHeader")
    public ResultEntity setExcelHeader(HttpServletRequest request,MultipartFile file) throws IOException {
        String excelConfigId=request.getParameter("excelConfigId");
        return excelComporeService.importExcelHeader(_NumberUtils.toLong(excelConfigId,0L),file.getInputStream());
    }
    /**
     * 导入报表
     * @return
     */
    @RequestMapping("/compare/importData")
    public ResultEntity importData(HttpServletRequest request,MultipartFile file) throws Exception {
        String taskId=request.getParameter("taskId");
        String reportId=request.getParameter("reportId");
        int isAppend= _NumberUtils.toInt(request.getParameter("isAppend"),0);
        //int sheetNumber= _NumberUtils.toInt(request.getParameter("sheetNumber"),1);
        //int dataStartRowNumber=_NumberUtils.toInt(request.getParameter("dataStartRowNumber"),1);
        excelComporeService.importData(_NumberUtils.toLong(taskId,0L),_NumberUtils.toLong(reportId,0L),file.getInputStream(),isAppend);
        return ResultEntity.ok();
    }

    /**
     * 导出比较结果
     * @return
     */
    @RequestMapping("/compare/exportCompareResult")
    public ResultEntity exportCompareResult(@RequestBody Map<String,Object> param, HttpServletResponse response) throws Exception {
        Long taskId=MapUtils.getLong(param,"taskId");
        //int sheetNumber= _NumberUtils.toInt(request.getParameter("sheetNumber"),1);
        //int dataStartRowNumber=_NumberUtils.toInt(request.getParameter("dataStartRowNumber"),1);
        return excelComporeService.exportCompareResult(taskId,response);
    }
    /**
     * 导出字段比较结果
     * @return
     */
//    @RequestMapping("/compare/compareColumn")
//    public void compareColumn(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        String taskId=request.getParameter("taskId");
//        //int sheetNumber= _NumberUtils.toInt(request.getParameter("sheetNumber"),1);
//        //int dataStartRowNumber=_NumberUtils.toInt(request.getParameter("dataStartRowNumber"),1);
//        excelComporeService.exportCompareColumnResult(Long.parseLong(taskId),response);
//    }

    @RequestMapping("/compare/insertToData")
    public ResultEntity insertToData(@RequestBody Map<String,Object> param){
        Long fromReportId= MapUtils.getLong(param,"fromReportId");
        Long toReportId= MapUtils.getLong(param,"toReportId");
        Long taskId=MapUtils.getLong(param,"taskId");
        excelComporeService.insertTo(taskId,fromReportId,toReportId);
        return ResultEntity.ok();
    }

//    @RequestMapping("/compare/test")
//    public ResultEntity test(){
//        excelComporeService.test();
//        return ResultEntity.ok();
//    }
//    @RequestMapping("/compare/setColumnTitle")
//    public ResultEntity setColumnTitle(@RequestBody Map<String,Object> param){
//        Long reportId= MapUtils.getLong(param,"reportId");
//        excelComporeService.setColumnTitle(reportId);
//        return ResultEntity.ok();
//    }

//    @RequestMapping("/compare/updateTo")
//    public ResultEntity updateTo(@RequestBody Map<String,Object> param){
//        String fromReportId= MapUtils.getString(param,"fromReportId");
//        String toReportId= MapUtils.getString(param,"toReportId");
//        Long taskId=MapUtils.getLong(param,"taskId");
//        excelComporeService.updateTo(taskId,_NumberUtils.toLong(fromReportId,0L),_NumberUtils.toLong(toReportId,0L));
//        return ResultEntity.ok();
//    }

    @RequestMapping("/compare/updateByUpdateConfigId")
    public ResultEntity doUpdate(@RequestBody(required = false) Map<String,Object> param){
        Long updateConfigId=MapUtils.getLong(param,"updateConfigId");
        excelComporeService.updateByUpdateConfigId(updateConfigId);
        return ResultEntity.ok();
    }


    /**
     * 更新统计表
     * @param param
     * @return
     */
    @RequestMapping("/ec/stat/updateData2")
    public ResultEntity updateData2(@RequestBody(required = false) Map<String,Object> param){
        Long taskId=MapUtils.getLong(param,"taskId");
        excelComporeService.staData2(taskId);
        return ResultEntity.ok();
    }

}
